package cn.wonhigh.o2o.openfire.plugins;

import java.util.Map;

import org.jivesoftware.util.JiveGlobals;
import org.jivesoftware.util.PropertyEventListener;

import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import cn.wonhigh.o2o.openfire.plugins.util.RedisTemplate;

/**
 * redis初始化器
 * 
 * @author wei.ss
 * @createDate 2015-03-09
 * @version 1.0
 * 
 */
public class RedisInitializer implements PropertyEventListener {

	// redis
	private static final String PLUGIN_REDIS_IP_NAME = "redis.server.ip";
	private static final String PLUGIN_REDIS_PORT_NAME = "redis.server.port";
	private String redisIp;
	private int redisPort;
	protected RedisTemplate redisTemplate;
	
	/**
	 * 初始化redis配置
	 */
	protected void initRedis() {
		redisIp = JiveGlobals.getProperty(PLUGIN_REDIS_IP_NAME);
		redisPort = JiveGlobals.getIntProperty(PLUGIN_REDIS_PORT_NAME, 6379);
		if (redisIp == null) {
			return;
		}
		JedisPoolConfig poolConfig = new JedisPoolConfig();
		JedisPool jedisPool = new JedisPool(poolConfig, redisIp, redisPort);
		redisTemplate = new RedisTemplate();
		redisTemplate.setRedisPool(jedisPool);
	}
	
	@Override
	public void propertySet(String property, Map<String, Object> params) {
		if (property.equals(PLUGIN_REDIS_IP_NAME)) {
			this.redisIp = (String) params.get("value");
		}
		if (property.equals(PLUGIN_REDIS_PORT_NAME)) {
			this.redisPort = Integer.valueOf((String) params.get("value"));
		}
		initRedis();
	}

	@Override
	public void propertyDeleted(String property, Map<String, Object> params) {
	}

	@Override
	public void xmlPropertySet(String property, Map<String, Object> params) {
	}

	@Override
	public void xmlPropertyDeleted(String property, Map<String, Object> params) {
	}

}
